package homeWork;

import java.util.Random;
import java.util.Scanner;

public class GuessNumber {

    //    用户可用猜测次数
    private int gameCount = 10;
    //    用户猜测次数
    private int userGameCount = 0;
    //    导入需要使用的库
    Random rm = new Random();
    Scanner sc = new Scanner(System.in);

    int getRandomInt() {
        return rm.nextInt(100);
    }

    int getUserInput() {
        int userInput;
        while (true) {
            try {
                System.out.println("请输入您猜测的数字：");
//                sc.nextLine();
                userInput = sc.nextInt();
                break;
            } catch (Exception e) {
                System.out.println("您输入的数字有误，清重新输入");
                sc.nextLine();
            }
        }
        return userInput;
    }

    void start() {
        int randomInt = getRandomInt();

        System.out.println("****************欢迎来到猜数字游戏!!!!******************");
        System.out.println();
        System.out.println();

        int userInput;
        while (true) {
            userInput = getUserInput();
            if (gameCount == 1) {
                System.out.println("您的猜测机会已用完，游戏结束！");
                break;
            }
            if (userInput > randomInt) {
                System.out.println("猜大了");
            } else if (userInput == randomInt) {
                System.out.println("恭喜您，猜对了！随机数是：" + randomInt);
                break;
            } else {
                System.out.println("猜小了");
            }
            gameCount -= 1;
            userGameCount += 1;
        }

    }

    public static void main(String[] args) {
        GuessNumber g = new GuessNumber();
        g.start();
//        g.getUserInput();
    }
}
